package br.com.yahoo.mau_mss.tutorjavafxgolfx.controller;

import static java.lang.Thread.sleep;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author mauricio.soares
 */
public class ConfirmacaoController implements Initializable {
  @FXML
  private TextField txCartao;
  @FXML
  private TextField txValidade;
  @FXML
  private TextField txSeguranca;
  @FXML
  private Button btConfirmar, btSair;

  /**
   * Initializes the controller class.
   * @param url
   * @param rb
   */
  @Override
  public void initialize(URL url, ResourceBundle rb) {

  }  
  
  @FXML
  private void handleConfirmarAction(ActionEvent event) {
    //processaConfirmacaoComThread(event);
    processaConfirmacaoComTask(event);
  }
  
  /**
   * Treads são recomendadas para rotinas curtas
   * @param event 
   */
  private void processaConfirmacaoComThread(final ActionEvent event) {
    Thread thread = new Thread() {
      @Override
      public void run() {
        try {
          sleep(5000);
        } catch (InterruptedException e) {
          Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, e);
        }
        JOptionPane.showMessageDialog(null, "Compra realizada com sucesso!");
        // necessário usar runLater uma vez que a thread do JavaFX pode ter sido afetada
        Platform.runLater(new Runnable() {
          @Override
          public void run() {
            CarrinhoApp.getStage().close();
            ItemApp.getStage().close();
            handleSairAction(event);
          }
        });
      };
    };
    thread.start();
  }
  
  /**
   * Task são recomendadas para rotinas maiores
   * @param event 
   */
  private void processaConfirmacaoComTask(final ActionEvent event) {
    Task<Void> task = new Task<Void>() {
      @Override
      protected Void call() throws Exception {
        updateMessage("Aguarde...");
        try {
          Thread.sleep(5000);
        }catch(InterruptedException e) {
           //Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, e); 
        }
        updateMessage("Confirmar");
        JOptionPane.showMessageDialog(null, "Compra realizada com sucesso!");
        // necessário usar runLater uma vez que a thread do JavaFX pode ter sido afetada
        Platform.runLater(new Runnable() {
          @Override
          public void run() {
            CarrinhoApp.getStage().close();
            ItemApp.getStage().close();
            handleSairAction(event);
          }
        });
        return null;
      }
    };
    this.btConfirmar.textProperty().bind(task.messageProperty());
    new Thread(task).start();
  }
  
  @FXML
  private void handleSairAction(ActionEvent event) {
    Node source = (Node)event.getSource(); 
    Stage stage  = (Stage)source.getScene().getWindow();
    stage.close();
  }
  
}
